package com.liberty.dasheen.front;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liberty.dasheen.config.shiro.realm.ShiroDbRealm;

@Controller
@RequestMapping("/front")
public class FrontController {

	private static final Logger log = LoggerFactory.getLogger(ShiroDbRealm.class);
	
	@RequestMapping("/home")
	public String homepage(ModelMap map) {
		map.put("pageType", "home");
		return "front/index";
	}
	
	/**
	 * @description 到消费页面
	 * @author Liberty
	 * @date 2017年8月27日 上午6:44:31
	 * @return
	 */
	@RequestMapping("/consume")
	public String toConsumePage(ModelMap map) {
		map.put("pageType", "consume");
		return "front/consume";
	}
	/**
	 * @description 到其它页面
	 * @author Liberty
	 * @date 2017年8月27日 上午6:44:31
	 * @return
	 */
	@RequestMapping("/others")
	public String toOthersPage(ModelMap map) {
		map.put("pageType", "others");
		return "front/others";
	}
	
	/**
	 * @description 到登录页面
	 * @author Liberty
	 * @date 2017年8月27日 上午2:59:50
	 * @return
	 */
	@RequestMapping("/loginpage")
	public String loginPage(ModelMap map) {
		map.put("pageType", "login");
		return "front/login";
	}
	
	/**
	 * @description 登录 
	 * @author Liberty
	 * @date 2017年8月27日 上午3:19:00
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String userCode, String password) {
		Subject user = SecurityUtils.getSubject();
		if (!user.isAuthenticated()) {
			UsernamePasswordToken upToken = new UsernamePasswordToken(userCode, password);
			// remember me
			upToken.setRememberMe(true);
			try {
				user.login(upToken);
			} catch (AuthenticationException e) {
				System.out.println("登录失败! Exception:" + e);
				return "redirect:/front/loginpage.html";
			}
		}
		return "redirect:/admin/home.html";
	}
}
